package com.bdtop.dbl.server.request;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.bdtop.dbl.model.DeviceInfo;
import com.bdtop.dbl.model.Position;
import com.bdtop.dbl.server.response.AllDeviceInfoResponse;
import com.bdtop.dbl.thirdparty.asynchttp.HttpPostRequest;
import com.bdtop.dbl.thirdparty.asynchttp.RequestCallback;
import com.bdtop.dbl.utils.Constants;
import com.bdtop.dbl.utils.DBLDebug;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;

/**
 * @ClassName: AllDeviceInfoRequest
 * @Description: TODO
 * @author: wangpeng
 * @date:2014-11-12
 */
public class AllDeviceInfoRequest extends HttpPostRequest {

	/**
	 * @param callback
	 */
	public AllDeviceInfoRequest(RequestCallback callback) {
		super(callback);
		getRequestParams().add(Constants.COMMAND, Constants.CMD_GET_ALL_DEVICE);
	}

	public void setParams(String sign) {
		getRequestParams().add(Constants.PARAM_LOGIN_SIGN, sign);
	}

	@Override protected String getUrl() {
		return Constants.SERVER_ADDRESS;
	}

	@Override protected Object parse(String responseString) {
		DBLDebug.logd("AllDeviceInfoRequest, response = " + responseString);

		 Gson gson = new Gson();
		 AllDeviceInfoResponse response = gson.fromJson(responseString,
		 AllDeviceInfoResponse.class);
		 return response;

//		try {
//			AllDeviceInfoResponse response = new AllDeviceInfoResponse();
////			 response.device_info
//			List<DeviceInfo> deviceInfoList = new ArrayList<DeviceInfo>();
//			
//			JSONObject jo = new JSONObject(responseString);
//			response.setStatus(String.valueOf(jo.get("b")));
//			response.setDesc(String.valueOf(jo.get("desc")));
//			
//			JSONArray ja = jo.getJSONArray("device_info");
//			for (int i = 0; i < ja.length(); i++) {
//				JSONObject jo2 = ja.getJSONObject(i);
//				DeviceInfo deviceInfo = new DeviceInfo();
//				deviceInfo.mdn = jo2.getString("mdn");
//				deviceInfo.name = jo2.getString("name");
//				deviceInfo.location_mode = jo2.getInt("location_mode");
//				deviceInfo.elec = jo2.getInt("elec");
//				deviceInfo.state = jo2.getInt("state");
//				deviceInfo.av = jo2.getInt("av");
//				deviceInfo.imei = jo2.getString("imei");
//				deviceInfo.power = jo2.getString("power");
//				deviceInfo.sos_phones = (String[])jo2.get("sos_phones");
//				Position position = new Position();
//				JSONObject jo3 = jo2.getJSONObject("pos");
//				position.lon = jo3.getDouble("lon");
//				position.lat = jo3.getDouble("lat");
//				deviceInfo.pos = position;
//				
//				
//				
//				
//				deviceInfo.addrname = jo2.getString("addrname");
//				
//			}
//			
//			return response;
//		} catch (JSONException e) {
//			e.printStackTrace();
//		}
//		return null;

	}

}
